#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:robot
@file: pre_order_tree1.py 
@version:
@time: 2024/01/24

借助栈，用python的列表的pop和append可以轻松实现
"""


class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


node1 = TreeNode(1)
node2 = TreeNode(2)
node3 = TreeNode(3)
node4 = TreeNode(4)
node5 = TreeNode(5)
node6 = TreeNode(6)
node1.left = node2
node1.right = node3
node2.left = node4
node3.left = node5
node3.right = node6


def visit_tree(root):
    stack = []
    stack.append(root)
    while stack:
        top = stack.pop()
        print(top.val)
        if top.right:
            stack.append(top.right)
        if top.left:
            stack.append(top.left)


visit_tree(node1)
